Skip to content

[DirectX] Match DXC's resource order in DX container #130233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

bogner
Copy link
Contributor

@bogner bogner commented Mar 7, 2025

DXC and the DXIL validator expect resources in a DX container to be specifically ordered CBuffers, Samplers, SRVs, and then UAVs. Match this behaviour so that we can pass the validator.

Fixes #130232.

DXC and the DXIL validator expect resources in a DX container to be
specifically ordered CBuffers, Samplers, SRVs, and then UAVs. Match this
behaviour so that we can pass the validator.

Fixes llvm#130232.
@llvmbot
Copy link
Member

llvmbot commented Mar 7, 2025

@llvm/pr-subscribers-llvm-binary-utilities

Author: Justin Bogner (bogner)

Changes

DXC and the DXIL validator expect resources in a DX container to be specifically ordered CBuffers, Samplers, SRVs, and then UAVs. Match this behaviour so that we can pass the validator.

Fixes #130232.


Full diff: https://github.com/llvm/llvm-project/pull/130233.diff

4 Files Affected:

  • (modified) llvm/include/llvm/BinaryFormat/DXContainer.h (+1-1)
  • (modified) llvm/lib/Target/DirectX/DXContainerGlobals.cpp (+57-37)
  • (added) llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll (+26)
  • (modified) llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll (+11)
diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index bd5a796c0b31c..28905e27837a7 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -320,7 +320,7 @@ ArrayRef<EnumEntry<ResourceKind>> getResourceKinds();
 
 #define RESOURCE_FLAG(Index, Enum) bool Enum = false;
 struct ResourceFlags {
-  ResourceFlags() {};
+  ResourceFlags() : Flags(0U) {};
   struct FlagsBits {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   };
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 5508af40663b1..c7a130a1f9c8a 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -186,51 +186,71 @@ void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
   DXILResourceTypeMap &DRTM =
       getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
 
-  for (const dxil::ResourceBindingInfo &RBI : DBM) {
+  auto MakeBinding =
+      [](const dxil::ResourceBindingInfo::ResourceBinding &Binding,
+         const dxbc::PSV::ResourceType Type, const dxil::ResourceKind Kind,
+         const dxbc::PSV::ResourceFlags Flags = dxbc::PSV::ResourceFlags()) {
+        dxbc::PSV::v2::ResourceBindInfo BindInfo;
+        BindInfo.Type = Type;
+        BindInfo.LowerBound = Binding.LowerBound;
+        BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
+        BindInfo.Space = Binding.Space;
+        BindInfo.Kind = static_cast<dxbc::PSV::ResourceKind>(Kind);
+        BindInfo.Flags = Flags;
+        return BindInfo;
+      };
+
+  for (const dxil::ResourceBindingInfo &RBI : DBM.cbuffers()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+    PSV.Resources.push_back(MakeBinding(Binding, dxbc::PSV::ResourceType::CBV,
+                                        dxil::ResourceKind::CBuffer));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.samplers()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+    PSV.Resources.push_back(MakeBinding(Binding,
+                                        dxbc::PSV::ResourceType::Sampler,
+                                        dxil::ResourceKind::Sampler));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.srvs()) {
     const dxil::ResourceBindingInfo::ResourceBinding &Binding =
         RBI.getBinding();
-    dxbc::PSV::v2::ResourceBindInfo BindInfo;
-    BindInfo.LowerBound = Binding.LowerBound;
-    BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
-    BindInfo.Space = Binding.Space;
 
     dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
-    dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
-    bool IsUAV = TypeInfo.getResourceClass() == dxil::ResourceClass::UAV;
-    switch (TypeInfo.getResourceKind()) {
-    case dxil::ResourceKind::Sampler:
-      ResType = dxbc::PSV::ResourceType::Sampler;
-      break;
-    case dxil::ResourceKind::CBuffer:
-      ResType = dxbc::PSV::ResourceType::CBV;
-      break;
-    case dxil::ResourceKind::StructuredBuffer:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
-                      : dxbc::PSV::ResourceType::SRVStructured;
-      if (IsUAV && TypeInfo.getUAV().HasCounter)
-        ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
-      break;
-    case dxil::ResourceKind::RTAccelerationStructure:
+    dxbc::PSV::ResourceType ResType;
+    if (TypeInfo.isStruct())
+      ResType = dxbc::PSV::ResourceType::SRVStructured;
+    else if (TypeInfo.isTyped())
+      ResType = dxbc::PSV::ResourceType::SRVTyped;
+    else
       ResType = dxbc::PSV::ResourceType::SRVRaw;
-      break;
-    case dxil::ResourceKind::RawBuffer:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
-                      : dxbc::PSV::ResourceType::SRVRaw;
-      break;
-    default:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
-                      : dxbc::PSV::ResourceType::SRVTyped;
-      break;
-    }
-    BindInfo.Type = ResType;
-
-    BindInfo.Kind =
-        static_cast<dxbc::PSV::ResourceKind>(TypeInfo.getResourceKind());
+
+    PSV.Resources.push_back(
+        MakeBinding(Binding, ResType, TypeInfo.getResourceKind()));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.uavs()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+
+    dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
+    dxbc::PSV::ResourceType ResType;
+    if (TypeInfo.getUAV().HasCounter)
+      ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
+    else if (TypeInfo.isStruct())
+      ResType = dxbc::PSV::ResourceType::UAVStructured;
+    else if (TypeInfo.isTyped())
+      ResType = dxbc::PSV::ResourceType::UAVTyped;
+    else
+      ResType = dxbc::PSV::ResourceType::UAVRaw;
+
+    dxbc::PSV::ResourceFlags Flags;
     // TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
     // with https://github.com/llvm/llvm-project/issues/104392
-    BindInfo.Flags.Flags = 0u;
+    Flags.Flags = 0u;
 
-    PSV.Resources.emplace_back(BindInfo);
+    PSV.Resources.push_back(
+        MakeBinding(Binding, ResType, TypeInfo.getResourceKind(), Flags));
   }
 }
 
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll
new file mode 100644
index 0000000000000..734149eec598e
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll
@@ -0,0 +1,26 @@
+; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s
+
+; Check that resources are emitted to the object in the order that matches what
+; the DXIL validator expects: CBuffers, Samplers, SRVs, and then UAVs.
+
+; CHECK: Resources:
+; CHECK: - Type: CBV
+; TODO:  - Type: Sampler
+; CHECK: - Type: SRVRaw
+; CHECK: - Type: UAVTyped
+
+target triple = "dxil-unknown-shadermodel6.0-compute"
+
+define void @main() #0 {
+  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
+      @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0t(
+          i32 2, i32 7, i32 1, i32 0, i1 false)
+  %srv0 = call target("dx.RawBuffer", i8, 0, 0)
+      @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i8_0_0t(
+          i32 1, i32 8, i32 1, i32 0, i1 false)
+  %cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
+      @llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)
+  ret void
+}
+
+attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
index ce67812c3988f..cea8ba2f067c1 100644
--- a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
+++ b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
@@ -6,6 +6,17 @@ target triple = "dxil-unknown-shadermodel6.0-compute"
 
 define void @main() #0 {
 
+  ; cbuffer : register(b2, space3) { float x; }
+; CHECK:        - Type:            CBV
+; CHECK:          Space:           3
+; CHECK:          LowerBound:      2
+; CHECK:          UpperBound:      2
+; CHECK:          Kind:            CBuffer
+; CHECK:          Flags:
+; CHECK:            UsedByAtomic64:  false
+  %cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
+      @llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)
+
   ; ByteAddressBuffer Buf : register(t8, space1)
 ; CHECK:        - Type:            SRVRaw
 ; CHECK:          Space:           1

@llvmbot
Copy link
Member

llvmbot commented Mar 7, 2025

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

Changes

DXC and the DXIL validator expect resources in a DX container to be specifically ordered CBuffers, Samplers, SRVs, and then UAVs. Match this behaviour so that we can pass the validator.

Fixes #130232.


Full diff: https://github.com/llvm/llvm-project/pull/130233.diff

4 Files Affected:

  • (modified) llvm/include/llvm/BinaryFormat/DXContainer.h (+1-1)
  • (modified) llvm/lib/Target/DirectX/DXContainerGlobals.cpp (+57-37)
  • (added) llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll (+26)
  • (modified) llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll (+11)
diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index bd5a796c0b31c..28905e27837a7 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -320,7 +320,7 @@ ArrayRef<EnumEntry<ResourceKind>> getResourceKinds();
 
 #define RESOURCE_FLAG(Index, Enum) bool Enum = false;
 struct ResourceFlags {
-  ResourceFlags() {};
+  ResourceFlags() : Flags(0U) {};
   struct FlagsBits {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   };
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 5508af40663b1..c7a130a1f9c8a 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -186,51 +186,71 @@ void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
   DXILResourceTypeMap &DRTM =
       getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
 
-  for (const dxil::ResourceBindingInfo &RBI : DBM) {
+  auto MakeBinding =
+      [](const dxil::ResourceBindingInfo::ResourceBinding &Binding,
+         const dxbc::PSV::ResourceType Type, const dxil::ResourceKind Kind,
+         const dxbc::PSV::ResourceFlags Flags = dxbc::PSV::ResourceFlags()) {
+        dxbc::PSV::v2::ResourceBindInfo BindInfo;
+        BindInfo.Type = Type;
+        BindInfo.LowerBound = Binding.LowerBound;
+        BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
+        BindInfo.Space = Binding.Space;
+        BindInfo.Kind = static_cast<dxbc::PSV::ResourceKind>(Kind);
+        BindInfo.Flags = Flags;
+        return BindInfo;
+      };
+
+  for (const dxil::ResourceBindingInfo &RBI : DBM.cbuffers()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+    PSV.Resources.push_back(MakeBinding(Binding, dxbc::PSV::ResourceType::CBV,
+                                        dxil::ResourceKind::CBuffer));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.samplers()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+    PSV.Resources.push_back(MakeBinding(Binding,
+                                        dxbc::PSV::ResourceType::Sampler,
+                                        dxil::ResourceKind::Sampler));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.srvs()) {
     const dxil::ResourceBindingInfo::ResourceBinding &Binding =
         RBI.getBinding();
-    dxbc::PSV::v2::ResourceBindInfo BindInfo;
-    BindInfo.LowerBound = Binding.LowerBound;
-    BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
-    BindInfo.Space = Binding.Space;
 
     dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
-    dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
-    bool IsUAV = TypeInfo.getResourceClass() == dxil::ResourceClass::UAV;
-    switch (TypeInfo.getResourceKind()) {
-    case dxil::ResourceKind::Sampler:
-      ResType = dxbc::PSV::ResourceType::Sampler;
-      break;
-    case dxil::ResourceKind::CBuffer:
-      ResType = dxbc::PSV::ResourceType::CBV;
-      break;
-    case dxil::ResourceKind::StructuredBuffer:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
-                      : dxbc::PSV::ResourceType::SRVStructured;
-      if (IsUAV && TypeInfo.getUAV().HasCounter)
-        ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
-      break;
-    case dxil::ResourceKind::RTAccelerationStructure:
+    dxbc::PSV::ResourceType ResType;
+    if (TypeInfo.isStruct())
+      ResType = dxbc::PSV::ResourceType::SRVStructured;
+    else if (TypeInfo.isTyped())
+      ResType = dxbc::PSV::ResourceType::SRVTyped;
+    else
       ResType = dxbc::PSV::ResourceType::SRVRaw;
-      break;
-    case dxil::ResourceKind::RawBuffer:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
-                      : dxbc::PSV::ResourceType::SRVRaw;
-      break;
-    default:
-      ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
-                      : dxbc::PSV::ResourceType::SRVTyped;
-      break;
-    }
-    BindInfo.Type = ResType;
-
-    BindInfo.Kind =
-        static_cast<dxbc::PSV::ResourceKind>(TypeInfo.getResourceKind());
+
+    PSV.Resources.push_back(
+        MakeBinding(Binding, ResType, TypeInfo.getResourceKind()));
+  }
+  for (const dxil::ResourceBindingInfo &RBI : DBM.uavs()) {
+    const dxil::ResourceBindingInfo::ResourceBinding &Binding =
+        RBI.getBinding();
+
+    dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
+    dxbc::PSV::ResourceType ResType;
+    if (TypeInfo.getUAV().HasCounter)
+      ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
+    else if (TypeInfo.isStruct())
+      ResType = dxbc::PSV::ResourceType::UAVStructured;
+    else if (TypeInfo.isTyped())
+      ResType = dxbc::PSV::ResourceType::UAVTyped;
+    else
+      ResType = dxbc::PSV::ResourceType::UAVRaw;
+
+    dxbc::PSV::ResourceFlags Flags;
     // TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
     // with https://github.com/llvm/llvm-project/issues/104392
-    BindInfo.Flags.Flags = 0u;
+    Flags.Flags = 0u;
 
-    PSV.Resources.emplace_back(BindInfo);
+    PSV.Resources.push_back(
+        MakeBinding(Binding, ResType, TypeInfo.getResourceKind(), Flags));
   }
 }
 
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll
new file mode 100644
index 0000000000000..734149eec598e
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll
@@ -0,0 +1,26 @@
+; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s
+
+; Check that resources are emitted to the object in the order that matches what
+; the DXIL validator expects: CBuffers, Samplers, SRVs, and then UAVs.
+
+; CHECK: Resources:
+; CHECK: - Type: CBV
+; TODO:  - Type: Sampler
+; CHECK: - Type: SRVRaw
+; CHECK: - Type: UAVTyped
+
+target triple = "dxil-unknown-shadermodel6.0-compute"
+
+define void @main() #0 {
+  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
+      @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0t(
+          i32 2, i32 7, i32 1, i32 0, i1 false)
+  %srv0 = call target("dx.RawBuffer", i8, 0, 0)
+      @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i8_0_0t(
+          i32 1, i32 8, i32 1, i32 0, i1 false)
+  %cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
+      @llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)
+  ret void
+}
+
+attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
index ce67812c3988f..cea8ba2f067c1 100644
--- a/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
+++ b/llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
@@ -6,6 +6,17 @@ target triple = "dxil-unknown-shadermodel6.0-compute"
 
 define void @main() #0 {
 
+  ; cbuffer : register(b2, space3) { float x; }
+; CHECK:        - Type:            CBV
+; CHECK:          Space:           3
+; CHECK:          LowerBound:      2
+; CHECK:          UpperBound:      2
+; CHECK:          Kind:            CBuffer
+; CHECK:          Flags:
+; CHECK:            UsedByAtomic64:  false
+  %cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
+      @llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)
+
   ; ByteAddressBuffer Buf : register(t8, space1)
 ; CHECK:        - Type:            SRVRaw
 ; CHECK:          Space:           1

Copy link
Member

@hekota hekota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

BindInfo.LowerBound = Binding.LowerBound;
BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
BindInfo.Space = Binding.Space;
BindInfo.Kind = static_cast<dxbc::PSV::ResourceKind>(Kind);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this conversion be safer done with a helper function or constructor? I couldn't find the definition for dxbc::PSV::ResourceKind but dxil::ResourceKind was just an enum. Seems like something that we could accidentally break ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dxbc::PSV::ResourceKind and dxil::ResourceKind happen to be the exact same enum. One is defined in DXContainerConstants and the other in DXILABI, but if they didn't match exactly we would have a serious problem.

This means that technically the static cast is safe - but it's definitely a break in abstraction. Arguably we should tie these two enums together in a clearer way, though I'm not sure exactly where that should best happen.

In any case, I think that should be out of scope of this PR - I am just moving this cast after all, not introducing it here.

Copy link
Contributor

@alsepkow alsepkow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@bogner bogner merged commit 368c7f7 into llvm:main Mar 24, 2025
15 checks passed
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
@damyanp damyanp removed this from HLSL Support Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DirectX] Resources in PSV0 need to be in a specific order to match validation
4 participants